package com.bilibili.controller;

import com.alibaba.fastjson.JSON;
import com.bilibili.annocation.CurUser;
import com.bilibili.entity.*;
import com.bilibili.exception.BusinessException;
import com.bilibili.service.PersonalService;
import com.bilibili.utils.CookieOperations;
import com.bilibili.utils.RedisOperations;
import com.bilibili.vo.CollectionVo;
import com.bilibili.vo.HistoryVo;
import com.bilibili.vo.PersonalData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.naming.CompositeName;

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Objects;

import static com.bilibili.constant.BusinessConstant.COOKIE_TOKEN_NAME;
import static com.bilibili.constant.BusinessConstant.REDIS_USER_PREFIX;


/**
 * 获取个人中心de controller接口
 */
@RestController
public class PersonalController {

    @Autowired
    PersonalService personalService;

    @Autowired
    private CookieOperations cookieOperations;
    @Autowired
    private RedisOperations redisOperations;

    /**
     * 根据用户id获取用户个人中心数据
     *
     * @param
     * @return 用户个人中心数据
     */
    @RequestMapping(value = "personal", method = RequestMethod.GET)
    public PersonalData getPersonalByuserId(@CurUser User user){
//        User user1 = new User();
//        user1.setId(1);
        PersonalData personalByuserId = personalService.getPersonalByuserId(user.getId());
        return personalByuserId;
    }


    /**
     * 根据用户id获取用户详细信息
     *
     * @param
     * @return 用户详细信息
     */
    @RequestMapping(value = "userInfo", method = RequestMethod.GET)
    public UserInfo getUserInfoByuserId(@CurUser User user){
//        User user1 = new User();
//        user1.setId(1);
        UserInfo userInfoByuserId = personalService.getUserInfoByuserId(user.getId());
        return userInfoByuserId;
    }

    /**
     * 用户详细信息
     *
     * @param
     * @return
     */
    @RequestMapping(value = "addUserInfo", method = RequestMethod.POST)
    public boolean addUserInfo(@RequestBody UserInfo userInfo,@CurUser User user){
        return personalService.addUserInfo(userInfo);
    }


    /**
     * 修改用户详细信息
     *
     * @param
     * @return
     */
    @RequestMapping(value = "modifyUserInfo", method = RequestMethod.POST)
    public boolean modifyUserInfo(@RequestBody UserInfo userInfo,@CurUser User user){
//        User user1 = new User();
//        user1.setId(1);
        return personalService.modifyUserInfo(userInfo,user.getId());
    }



    /**
     * 根据用户id获取我的空间信息
     *
     * @param
     * @return 用户详细信息
     */
    @RequestMapping(value = "space", method = RequestMethod.GET)
    public Space getSpaceByuserId(@CurUser User user){
//        User user1 = new User();
//        user1.setId(1);
        Space spaceByuserId = personalService.getSpaceByuserId(user.getId());
        return spaceByuserId;
    }

    /**
     * 根据用户id获取我的硬币记录
     *
     * @param
     * @return 用户详细信息
     */
    @RequestMapping(value = "mycoin", method = RequestMethod.GET)
    public List<MyCoin> getAllCoinRecordByuserId(@CurUser User user){
//        User user1 = new User();
//        user1.setId(1);
        return personalService.getAllCoinRecord(user.getId());
    }


    /**
     * 添加用户经验值
     *
     * @param
     * @return 用户详细信息
     */
//    @RequestMapping(value = "addExp", method = RequestMethod.POST, params = {"exp"})
//    public boolean addExp(@RequestParam("userId") Integer userId,
//                        @RequestParam("exp") Integer exp){
//       return personalService.addExp(userId, exp);
//    }

    @RequestMapping(value = "addExp", method = RequestMethod.POST, params = {"exp"})
    public boolean addExp(@CurUser User user,
                          @RequestParam("exp") Integer exp){
//        User user1 = new User();
//        user1.setId(1);
        return personalService.addExp(user.getId(), exp);
    }


    /**
     * 添加用户硬币
     *
     * @param
     * @return 用户详细信息
     */
    @RequestMapping(value = "addCoin", method = RequestMethod.POST, params = {"coin"})
    public boolean addCoin(@CurUser User user,
                          @RequestParam("coin") Integer coin){
//        User user1 = new User();
//        user1.setId(1);
        return personalService.addCoin(user.getId(), coin);
    }


    /**
     * 查询我的历史记录
     * @return
     */
//    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"fetch_type=history"})
//    public List<Video> getHistoryVideos(@CurUser User user, @RequestParam(name = "ids", required = false) Integer[] tempHistoryVedioVids) {
//        return personalService.getHistoryVideos(tempHistoryVedioVids, user);
//    }

    /**
     * 判断是否获得每日观看视频奖励
     * @param
     * @return
     */
    @RequestMapping(value="watchAwards",method = RequestMethod.GET)
    boolean isAchieveWatchAward(@CurUser User user){
//        User user1 = new User();
//        user1.setId(1);
        return personalService.isAchieveWatchAward(user.getId());
    }


    /**
     * 获得每日投币奖励
     * @param
     * @return
     */
    @RequestMapping(value="coinAwards",method = RequestMethod.GET)
    int getCoinAward(@CurUser User user){
//        User user1 = new User();
//        user1.setId(1);
        return personalService.getCoinAward(user.getId());
    }


    /**
     * 获得收藏记录
     *
     * @return
     */
    @RequestMapping(value="getCollection",method = RequestMethod.GET)
    List<CollectionVo> getCollectionRecord(@CurUser User user){
//        User user1 = new User();
//        user1.setId(1);
        return personalService.getCollectionRecord(user.getId());
    }

    /**
     * 获得历史记录
     *
     * @return
     */
    @RequestMapping(value="getHistory",method = RequestMethod.GET)
    List<HistoryVo> getHistoryRecord(@CurUser User user){
//        User user1 = new User();
//        user1.setId(1);
        return personalService.getHistoryRecord(user.getId());
    }






}
